package com.project8.book;

import java.io.Serializable;
import java.util.ArrayList;
import android.app.Activity;


/** A book is essentially a collection of Chapters */
/** A book is essentially a collection of Pages */
 
public class Book extends Activity implements Serializable{
	

	private static final long serialVersionUID = -8284303998010340950L;
	
	long numPages = 0;				//Total number of pages in the book
	String bookTitle = "";
	String chapterName = "";
	boolean generated = false;
	
	static int book_ID = 0;
	int thisBookID;
		
	TableOfBookmarks table_bookmarks = new TableOfBookmarks();
	Notebook notebook = new Notebook();
	
	// Page variables which will be used to set the content of the pages in layout so that page flipping will be possible in both directions.
	Page previousPage;
	Page currentPage;
	Page nextPage;
	
	ArrayList<Chapter> chaptersInBook = new ArrayList<Chapter>();
	
	ArrayList<Page> pagesInBook = new ArrayList<Page>();
	
	public Book(String title)
	{
		
		//Set Book Title
		this.setBookTitle(title);
		thisBookID = book_ID++;
			
	}
		
	
	/** While reading a book, this method allows you to modify the Table of Bookmarks. 
		This method will be called every time a page is bookmarked */
	public void addToTableOfBookmarks(Bookmark bm)
	{
		table_bookmarks.add(bm);
	}
	
	/** While reading a book, this method allows you to modify the Table of Bookmarks. 
		This method will be called every time a page is un-bookmarked */
	public void removeFromTableOfBookmarks(Bookmark bm)
	{
		table_bookmarks.remove(bm);
	}
	
	/** Add a Chapter or section to this Book */
	public void addChapter(Chapter chapter)
	{
		chaptersInBook.add(chapter);
		
		addMultiplePages(chapter.getPages());
	}
	
	/** Add a Page to this Book */
	public void addPage(Page page)
	{
		pagesInBook.add(page);
		
		sortPages();
	}
	
	/** Add a Page to this Book */
	public void addMultiplePages(ArrayList<Page> pages)
	{
		for(int p = 0; p < pages.size(); p++)
			pagesInBook.add(pages.get(p));
		
		sortPages();
	}
	
	public ArrayList<Chapter> getChapters()
	{
		return chaptersInBook;
	}
	
	public ArrayList<Page> getPages()
	{
		return pagesInBook;
	}
	
	public long numChapters()
	{
		return chaptersInBook.size();
	}
	
	public long numPages()
	{
		return pagesInBook.size();
	}
	
	/** Sorts the Pages in this Book so that they are always in order of increasing page number */
	private void sortPages()
	{
		for(int i = 0; i < pagesInBook.size() - 1; i++)
			for(int j = i; j < pagesInBook.size(); j++)
			{
				if(((Page)pagesInBook.get(j)).getPageNum() < ((Page)pagesInBook.get(i)).getPageNum())
				{
					Page temp = (Page)pagesInBook.get(i);
					pagesInBook.set(i, (Page)pagesInBook.get(j));
					pagesInBook.set(j, temp);
				}
			}
	}
	
	public TableOfBookmarks getTableOfBookmarks()
	{
		return table_bookmarks;
	}
	
	public void setCurrentPage(Page page)
	{
		currentPage = page;
	}
	
	public Page getCurrentPage()
	{
		return currentPage;
	}
	
	public String getBookTitle() {
		return bookTitle;
	}

	public void setBookTitle(String bookTitle) {
		this.bookTitle = bookTitle;
	}
	
	public Chapter getChapter()
	{
		return this.getCurrentPage().getChapter();
	}
	
	public String getChapterName()
	{
		return this.getCurrentPage().getChapterName();
	}
	public String getPageName()
	{
		return this.getCurrentPage().getPageName();
	}
	
	public String getFolderName()
	{
		return this.getCurrentPage().getChapter().getFolderName();
	}
	public Chapter findChapter(String chapterName)
	{
		for(int c = 0; c < chaptersInBook.size(); c++)
		{
			Chapter ch = chaptersInBook.get(c);
			
			if(ch.getName().equalsIgnoreCase(chapterName))
			{
				return ch;
			}
		}
		
		return null;
	}
	
	public boolean isGenerated() {
		return generated;
	}


	public void setGenerated(boolean generated) {
		this.generated = generated;
	}
	
	public void setChaptersInBook(ArrayList<Chapter> chaptersInBook) 
	{
		//Add Each Chapter to Book
		for(int i = 0; i < chaptersInBook.size(); i++)
		{			
			this.addChapter(chaptersInBook.get(i));	
		}
	}
	
	public Chapter getLongestChapter()
	{
		Chapter longest = this.getChapter();
		
		for(int c = 0; c < chaptersInBook.size(); c++)
		{
			Chapter chap = chaptersInBook.get(c);
			
			if(chap.numPages() > longest.numPages())
				longest = chap;
		}
		
		return longest;
	}
	
	public Notebook getNotebook()
	{
		return notebook;
	}
	
	public int getBookID()
	{
		return thisBookID;
	}

}
